#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <linux/input.h>
#include <stdio.h>  
#include <stdbool.h>  
#include <stdint.h>  
#include <malloc.h>  
#include <unistd.h>  
#include <stdlib.h>  
#include <string.h>  
#include <getopt.h>  
#include <fcntl.h>  
#include <ctype.h>  
#include <errno.h>  
#include <limits.h>  
#include <time.h> 
#include <locale.h>  
#include <signal.h>  
#include <pthread.h>
#include <sys/unistd.h>  
#include <sys/stat.h>  
#include <sys/types.h>  
#include <sys/socket.h>
#include <sys/mman.h>
#include <linux/input.h>
#include <linux/fb.h>
#include <arpa/inet.h>
#include <termios.h> 
//自己写的头文件，用来存数组和字模
#include "1.h"
 
//全局变量
int fd = 0;  //屏幕的文件描述符
int *plcd = NULL;  //映射后的屏幕的首地址
int pages=999;//页数
int nums=0;//总价格


//函数声明
//在指定的坐标点显示一个指定的颜色
void lcd_draw_point(int x,int y,int color);
//初始化屏幕
void lcd_init();
//关闭屏幕
void lcd_close();
//把屏幕刷成指定的颜色
void lcd_clear(int color);
//屏幕指定区域画指定颜色
void lcd_clear2(int x,int y,int x1,int y1,int color);
//可以在指定位置显示一个指定大小指定颜色的矩形
void lcd_draw_rect(int x0,int y0,int w,int h,int color);
//可以在指定位置显示一个指定大小指定颜色的圆形
// void lcd_draw_circle(int x0,int y0,int r,int color);
//函数可以显示指定的BMP图片到指定的位置
void lcd_draw_bmp(char *name,int x0,int y0);
//画收款码
void lcd_draw_bmp2(char *name,int x0,int y0);
//汉字
void lcd_draw_word( char* data,int x0, int y0, int w, int h, int color);
//48数字
void lcd_draw_num(int num,int x0,int y0,int color);
//16数字
void lcd_draw_num2(int num,int x0,int y0,int color);
//判断手指触摸点是否在指定的范围内
int get_touch();
//获取手指的滑动方向(1,2,3,4代表上下左右)
int get_touch2();
//画竖线
void lcd_draw_pow(int x);
//显示控件
void lcd_draw_button();
//在指定的槽内输出订单信息
void lcd_drwa_menu(int n,int y);
//button控件
void button();
//打印第n页
void lcd_draw_page(int p);
//初始化套接字用于与服务器通信
int Init_TcpSocket();


/*
    Init_TcpSocket:初始化套接字用于与服务器通信
*/
int Init_TcpSocket()
{
    //创建流式套接字
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    if(sockfd == -1)
    {
        perror("创建流式套接字失败,程序结束");
        exit(0);
    }

    int on = 1;
    //端口以及IP地址复用
    setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(1));

    //连接到虚拟机中
    struct sockaddr_in sin;
    bzero((char *)&sin,sizeof(sin));
    sin.sin_family = AF_INET;
    inet_pton(AF_INET,"192.168.31.27",&sin.sin_addr);  //IP地址必须与服务器真实的IP地址一致
    sin.sin_port = htons(6666);
    int ret = connect(sockfd,(struct sockaddr*)&sin,sizeof(sin));
    if(ret == -1)
    {
        perror("连接服务器失败,程序结束");
        close(sockfd);
        exit(0);
    }
    
    printf("连接服务器成功!\n");

    return sockfd;
}

//初始化屏幕
void lcd_init()
{
	//1.打开屏幕文件
	fd = open("/dev/fb0",O_RDWR);
	if(fd == -1)
	{
		printf("open lcd failed!\n");
		return ;
	}
	//2.映射屏幕到内存
	plcd = mmap(NULL, 800*480*4,PROT_READ | PROT_WRITE, MAP_SHARED,fd, 0);
	if(plcd == MAP_FAILED)
	{
		printf("mmap lcd failed!\n");
		close(fd);
		return ;
	}
}

//关闭屏幕
void lcd_close()
{
	//3.解映射
	munmap(plcd, 800*480*4);
	//4.关闭屏幕
	close(fd);
}

//在指定的坐标点显示一个指定的颜色
void lcd_draw_point(int x,int y,int color)
{
	if(x<0 || x>=800 || y<0 || y>=480)
	{
		printf("bro,your point out of the Screen!\n");
		return;
	}
	*(plcd+800*y+x) = color;
}

//把屏幕刷成指定的颜色
void lcd_clear(int color)
{
	int x,y;
	for(y=0;y<480;y++)
	{
		for(x=0;x<800;x++)
		{
			lcd_draw_point(x,y,color);
		}
	}
}

//可以在指定位置显示一个指定大小指定颜色的矩形
void lcd_draw_rect(int x0,int y0,int w,int h,int color)
{
	int x,y;
	for(y=y0;y<y0+h;y++)
	{
		for(x=x0;x<x0+w;x++)
		{
			lcd_draw_point(x,y,color);
		}
	}
}

//函数可以显示指定的BMP图片到指定的位置
void lcd_draw_bmp(char *name,int x0,int y0)
{
	//a.打开图片文件
	int fd_bmp = open(name,O_RDWR);
	if(fd_bmp == -1)
	{
		printf("open %s error!\n",name);
		return ;
	}
	//b.读取图片所有的像素点信息
	char buf[800*480*3+138] = {0};
	read(fd_bmp,buf,800*480*3+138);
	int w = buf[21] <<24 | buf[20] <<16 | buf[19] << 8 | buf[18];
	int h = buf[25] <<24 | buf[24] <<16 | buf[23] << 8 | buf[22];
	printf("name:%s,w = %d,h=%d\n",name,w,h);
	//c.关闭图片
	close(fd_bmp);
	//d.解析像素点信息
	int x,y;
	char a=0,r,g,b;
	int color;
	int p = 138;
	for(y=h-1;y>=0;y--)  //图片有h行
	{
		for(x=0;x<w;x++) //每一行w个点
		{
			b=buf[p++];
			g=buf[p++];
			r=buf[p++];
			color = a<<24 | r<<16 | g<<8 | b;
			lcd_draw_point(x+x0,y+y0,color);
		}
		if((w*3)%4 != 0)
		{
			p=p+4- (w*3)%4;
		}
	}
}

//画汉字
void lcd_draw_word( char* data,int x0, int y0, int w, int h, int color)
{
	int i,x,y;
	for(y=0;y<h;y++) //文字有h行
	{
		for(x=0;x<w/8;x++) //当前文字一行w个点,需要w/8字节
		{
			//解析word1[y*w/8+x]对应的字节
			for(i=7;i>=0;i--)
			{
				if(data[y*w/8+x] & (1<<i))
				{
					//word1[y*w/8+x]的第i个bit是1
					lcd_draw_point(8*x+7-i+x0,y+y0,color);
				}
			}
		}
	}
} 

//画48字体的数字
void lcd_draw_num(int num,int x0,int y0,int color)
{
	int a,b,c,d;
	a = (num%10000)/1000;
	b = (num%1000)/100;
	c = (num%100)/10;
	d = num%10;
	if(num>=1000)
	{
		lcd_draw_word(dig[a],x0,y0,32,64,color);
		lcd_draw_word(dig[b],x0+24,y0,32,64,color);
		lcd_draw_word(dig[c],x0+64,y0,32,64,color);
		lcd_draw_word(dig[d],x0+96,y0,32,64,color);
	}else if(num >=100)
	{
		lcd_draw_word(dig[b],x0,y0,32,64,color);
		lcd_draw_word(dig[c],x0+32,y0,32,64,color);
		lcd_draw_word(dig[d],x0+64,y0,32,64,color);
	}else if(num >=10)
	{
		lcd_draw_word(dig[c],x0,y0,32,64,color);
		lcd_draw_word(dig[d],x0+32,y0,32,64,color);
	}else if(num >= 0)
	{
		lcd_draw_word(dig[d],x0,y0,32,64,color);
	}
}

//画16字体的数字
void lcd_draw_num2(int num,int x0,int y0,int color)
{
	int a,b,c,d;
	a = (num%10000)/1000;
	b = (num%1000)/100;
	c = (num%100)/10;
	d = num%10;
	if(num>=1000)
	{
		lcd_draw_word(dig1[a],x0,y0,16,21,color);
		lcd_draw_word(dig1[b],x0+16,y0,16,21,color);
		lcd_draw_word(dig1[c],x0+32,y0,16,21,color);
		lcd_draw_word(dig1[d],x0+48,y0,16,21,color);
	}else if(num >=100)
	{
		lcd_draw_word(dig1[b],x0,y0,16,21,color);
		lcd_draw_word(dig1[c],x0+16,y0,16,21,color);
		lcd_draw_word(dig1[d],x0+32,y0,16,21,color);
	}else if(num >=10)
	{
		lcd_draw_word(dig1[c],x0,y0,16,21,color);
		lcd_draw_word(dig1[d],x0+16,y0,16,21,color);
	}else if(num >= 0)
	{
		lcd_draw_word(dig1[d],x0,y0,16,21,color);
	}
}

//指定横坐标画线
void lcd_draw_pow(int x)
{
    for(int y1 = 0;y1<480;y1++)
    {
        int x1 = x;
        lcd_draw_point(x1,y1, 0x00000000);
    }

}

//画整体按钮布局，以及其他
void lcd_draw_button()
{
	//画button,上下
		lcd_draw_rect(260,430,150,50,0x0000FF00);
		for (int i=0;i<=2;i++)
		{
			lcd_draw_word(f1[i],270+32*i,438,32,33,0x00FFFF00);
		}
		
		lcd_draw_rect(490,430,150,50,0x0000FF00);
		for (int i=0;i<=2;i++)
		{
			lcd_draw_word(f2[i],500+32*i,438,32,33,0x00FFFF00);
		}

        //清空菜单
        lcd_draw_rect(590,35,150,50,0x0000FF00);
		for (int i=0;i<=3;i++)
		{
			lcd_draw_word(f3[i],600+32*i,38,32,33,0x00FFFF00);
		}
		//画温馨提示
		for(int t1=0;t1<=4;t1++)
		{
			lcd_draw_word(g1[t1],3+24*t1,350,24,21,0x00000000);
		}
		for(int t2=0;t2<=5;t2++)
		{
			lcd_draw_word(g1[t2+5],3+24*t2,375,24,21,0x00000000);
		}
		for(int t3=0;t3<=5;t3++)
		{
			lcd_draw_word(g1[t3+11],3+24*t3,400,24,21,0x00000000);
		}
        //付款
        lcd_draw_rect(0,430,150,50,0x0000FFFF);
		for (int i=0;i<=1;i++)
		{
			lcd_draw_word(f4[i],30+32*i,435,32,33,0x00FF0000);
		}
		lcd_draw_pow(150);
		

		//画共计
		for( int y=0;y < 3; y++)
		{
			lcd_draw_word(a1[y],0+24*y,3,24,21,0x00000000);
		}
		//标题
		for (int c=0;c<=5;c++)
		{
			lcd_draw_word(bdc[c],200+64*c,10,64,64,0x00000000);
		}
		//组名
		for (int gz=0;gz<=2;gz++)
		{
			lcd_draw_word(sgz[gz],680+16*gz,435,16,33,0x000000FF);
		}
		lcd_draw_word(zu[0],680+16*3,438,32,33,0x000000FF);
		
}

//将指定位置刷白,x,y为起始位置x1,y1为终点位置
void lcd_clear2(int x,int y,int x1,int y1,int color)
{
    for ( ;y < y1; y++)
    {
        for (;x < x1; x++)
        {
            lcd_draw_point(x, y, color);
        }
    }
}

//画收款码
void lcd_draw_bmp2(char *name,int x0,int y0)
{
	//a.打开图片文件
	int fd_bmp = open(name,O_RDWR);
	if(fd_bmp == -1)
	{
		printf("open %s error!\n",name);
		return ;
	}
	//b.读取图片所有的像素点信息
	char buf[800*480*3+54] = {0};
	read(fd_bmp,buf,800*480*3+54);
	int w = buf[21] <<24 | buf[20] <<16 | buf[19] << 8 | buf[18];
	int h = buf[25] <<24 | buf[24] <<16 | buf[23] << 8 | buf[22];
	printf("name:%s,w = %d,h=%d\n",name,w,h);
	//c.关闭图片
	close(fd_bmp);
	//d.解析像素点信息
	int x,y;
	char a=0,r,g,b;
	int color;
	int p = 54;
	for(y=h-1;y>=0;y--)  //图片有h行
	{
		for(x=0;x<w;x++) //每一行w个点
		{
			b=buf[p++];
			g=buf[p++];
			r=buf[p++];
			color = a<<24 | r<<16 | g<<8 | b;
			lcd_draw_point(x+x0,y+y0,color);
		}
		if((w*3)%4 != 0)
		{
			p=p+4- (w*3)%4;
		}
	}
}

//判断手指触摸点是否在指定的范围内
int get_touch()
{
	//1.打开触摸屏文件
	int fd_touch = open("/dev/input/event0",O_RDWR);
	if(fd_touch == -1)
	{
		printf("open touch error!\n");
		return -1;
	}
	
	struct input_event  ev; //定义一个变量,保存读取到的事件
	int x,y;
	while(1)
	{
		//2.读取输入事件
		read(fd_touch,&ev,sizeof(ev));  
		//3.解析输入事件
		if(ev.type == EV_ABS && ev.code == ABS_X)
		{
			x = ev.value;
		}
		if(ev.type == EV_ABS && ev.code == ABS_Y)
		{
			y = ev.value;
		}
		if(ev.type == EV_KEY && ev.code == BTN_TOUCH && ev.value == 0)
		{
			break;
		}
	}
	//4.关闭触摸屏
	close(fd_touch);
	x = x/1.28;
	y = y/1.25;
	printf("x = %d,y = %d\n",x,y);
	
	//判断xy是否在某一个指定的范围内
	if(x>=0 && x<100 && y>=400 && y< 480)
	{
		return 1;
	}else if(x>=700 && x< 800 && y>=400 && y< 480)
	{
		return 2;
	}else 
	{
		return 3;
	}
	
}

//判断手指触摸点是否在指定的范围内
int get_touch2()
{
	//1.打开触摸屏文件
	int fd_touch = open("/dev/input/event0",O_RDWR);
	if(fd_touch == -1)
	{
		printf("open touch error!\n");
		return -1;
	}
	
	struct input_event  ev; //定义一个变量,保存读取到的事件
	int x0=-1,y0=-1; 	//起点
	int x,y;  	//终点
	
	while(1)
	{
		//2.读取输入事件
		read(fd_touch,&ev,sizeof(ev));  
		//3.解析输入事件
		if(ev.type == EV_ABS && ev.code == ABS_X)
		{
			if(x0 == -1)
			{
				x0 = ev.value;  //只会被赋值一次
			}
			x = ev.value;
		}
		if(ev.type == EV_ABS && ev.code == ABS_Y)
		{
			if(y0 == -1)
			{
				y0 = ev.value;  //只会被赋值一次
			}
			y = ev.value;
		}
		if(ev.type == EV_KEY && ev.code == BTN_TOUCH && ev.value == 0)
		{
			break;
		}
	}
	//4.关闭触摸屏
	close(fd_touch);
	printf("x0 = %d,y0 = %d\n",x0,y0);
	printf("x = %d,y = %d\n",x,y);
	
	//起点和终点的距离短,判断为点击
	if((x-x0)*(x-x0) + (y-y0)*(y-y0) <= 80*80)
	{
		x = x/1.28;
		y = y/1.25;
		//判断xy是否在某一个指定的范围内
		//上一页按钮 
		if(x>=260 && x<= 410 && y>=430 && y<=480)
		{
			return 13;
		}
		//下一页按钮
		else if(x>=490 && x<= 640 && y>=430 && y<=480)
		{
			return 14;
		}
		//清空菜单按钮
		else if(x>=590 && x<= 740 && y>=35 && y<=85)
		{
			return 15;
		}
		//下单按钮 
		else if(x>0 && x< 150 && y>430 && y<480) 
		{
			return 17;
        }
	//点菜加1
		//位置1
		else if(x>220 && x< 300 && y>95 && y<235)
		{
			flag[0]++;
			return 18;
		}
		//位置2
		else if(x>400 && x< 480 && y>95 && y<235)
		{
			flag[1]++;
			return 19;
		}
		//位置3
		else if(x>580 && x< 660 && y>95 && y<235)
		{
			flag[2]++;
			return 20;
		}
		// 位置4
		else if(x>220 && x< 300 && y>265 && y<405)
		{
			flag[3]++;
			return 21;
		}
		// 位置5
		else if(x>400 && x< 480 && y>265 && y<405)
		{
			flag[4]++;
			return 22;
		}
		// 位置6
		else if(x>580 && x< 660 && y>265 && y<405)
		{
			flag[5]++;
			return 23;
		}
	//点菜减1
		//位置1
		else if(x>300 && x< 380 && y>95 && y<235)
		{
			return 24;
		}
		//位置2
		else if(x>480 && x< 560 && y>95 && y<235)
		{
			return 25;
		}
		//位置3
		else if(x>660 && x< 740 && y>95 && y<235)
		{
			return 26;
		}
		// 位置4
		else if(x>300 && x< 380 && y>265 && y<405)
		{
			return 27;
		}
		// 位置5
		else if(x>480 && x< 560 && y>265 && y<405)
		{
			return 28;
		}
		// 位置6
		else if(x>660 && x< 740 && y>265 && y<405)
		{
			return 29;
		}
	}
}

//实现button控件功能
void button()
{
	//菜价
		int x1=20;
		int x2=30;
		int x3=60;
		int x4=110;
		int x5=18;
		int x6=30;
		int x7=12;
		int x8=88;
		int x9=15;
		int x10=28;
		int x11=88;
		int x12=88;
		int x13=38;
		int x14=66;
		int x15=88;
		int x16=50;
		int x17=10;
		int x18=999;
	//获取手指的点击结果
		int r1= get_touch2();
		printf("r1 = %d\n",r1);
		//上一页
		if(r1==13)
		{
			pages--;
			// lcd_clear2(425,416,457,480,0x00FFFFFF);
			lcd_clear(0x00FFFFFF);
			if(pages%3==0)
			{
				lcd_draw_page(1);
			}
			if(pages%3==1)
			{
				lcd_draw_page(2);
			}
			if(pages%3==2)
			{
				lcd_draw_page(3);
			}
        }
		//下一页 
		if(r1==14)
		{
            pages++;
			// lcd_clear2(425,416,457,480,0x00FFFFFF);
			lcd_clear(0x00FFFFFF);
			if(pages%3==0)
			{
				lcd_draw_page(1);
			}
			if(pages%3==1)
			{
				lcd_draw_page(2);
			}

			if(pages%3==2)
			{
				lcd_draw_page(3);
        	}
		}
        //清空菜单 
		if(r1==15)
		{
			nums=0;
            //初始化屏幕
	        lcd_init();
	        //先把屏幕刷成白色
	        lcd_clear(0x00FFFFFF);
			lcd_draw_button();
            // 第一页菜单
	        for (int i = 0; i <= 2; i++) 
			{
				lcd_draw_bmp(pics1[i],220+180*i,95);
			} 
                lcd_draw_bmp(pics1[3],220+180*0,265);
				lcd_draw_bmp(pics1[4],220+180*1,265);
				lcd_draw_bmp(pics1[5],220+180*2,265);
				lcd_clear2(425,416,457,480,0x00FFFFFF);
				lcd_draw_num(1,428,416,0x0000FF00);
			for (int no0 = 0; no0 <= 6; no0++)
				{
					lcd_draw_word(z0[no0],220+24*no0,235,24,21,0x00000000);
				}
				for (int no1 = 0; no1 <= 6; no1++)
				{
					lcd_draw_word(z1[no1],400+24*no1,235,24,21,0x00000000);
				}
				for (int no2 = 0; no2 <= 6; no2++)
				{
					lcd_draw_word(z2[no2],580+24*no2,235,24,21,0x00000000);
				}
				for (int no3 = 0; no3 <= 7; no3++)
				{
					lcd_draw_word(z3[no3],200+24*no3,405,24,21,0x00000000);
				}
				for (int no4 = 0; no4 <= 5; no4++)
				{
					lcd_draw_word(z4[no4],400+24*no4,405,24,21,0x00000000);
				}
				for (int no5 = 0; no5 <= 6; no5++)
				{
					lcd_draw_word(z5[no5],580+24*no5,405,24,21,0x00000000);
				}
				lcd_draw_num2(nums,72,3,0x00000000);
        }
        // 下单 
		if(r1==17)
		{
            //初始化屏幕
	        lcd_init();
	        //先把屏幕刷成白色
	        lcd_clear(0x00FFFFFF);
            //打印出付款码
			lcd_draw_bmp2(pics0[0],175,100);
            lcd_draw_bmp2(pics0[1],425,100);
			//回到点单
			lcd_draw_rect(590,35,150,50,0x0000FF00);
			for (int aa=0;aa<=3;aa++)
			{
				lcd_draw_word(dd[aa],600+32*aa,38,32,33,0x00FFFF0F);
			}
			//包得吃
			for (int i=0;i<=11;i++)
			{
				lcd_draw_word(a4[i],300+24*i,340,24,21,0x00000000);
			}
			//今天你得吃了吗？
			for (int i1=0;i1<=7;i1++)
			{
				lcd_draw_word(dc[i1],332+24*i1,364,24,21,0x00000000);
			}
			//总额
			for( int y=0;y < 3; y++)
			{
			lcd_draw_word(a1[y],0+24*y,3,24,21,0x00000000);
			}
			lcd_draw_num2(nums,72,3,0x00000000);
        }
		//点菜加1
		if(r1==18)
			{
			if(pages%3==0)
			{
				nums+=x1;
				lcd_draw_page(1);
				flag[0]++;
			}
			if(pages%3==1)
			{
				nums+=x7;
				lcd_draw_page(2);
				flag[1]++;
			}
			if(pages%3==2)
			{
				nums+=x13;
				lcd_draw_page(3);
				flag[2]++;
			}
        }
		if(r1==19)
		{
            printf("2");
			if(pages%3==0)
			{
				nums+=x2;
				lcd_draw_page(1);
				flag[3]++;
			}
			if(pages%3==1)
			{
				nums+=x8;
				lcd_draw_page(2);
				flag[4]++;
			}
			if(pages%3==2)
			{
				nums+=x14;
				lcd_draw_page(3);
				flag[5]++;
			}
        }
		if(r1==20)
		{
			if(pages%3==0)
			{
				nums+=x3;
				lcd_draw_page(1);
				flag[6]++;
			}
			if(pages%3==1)
			{
				nums+=x9;
				lcd_draw_page(2);
				flag[7]++;
			}
			if(pages%3==2)
			{
				nums+=x15;
				lcd_draw_page(3);
				flag[8]++;
			}
        }
		if(r1==21)
		{
			if(pages%3==0)
			{
				nums+=x4;
				lcd_draw_page(1);
				flag[9]++;
			}
			if(pages%3==1)
			{
				nums+=x10;
				lcd_draw_page(2);
				flag[10]++;
			}
			if(pages%3==2)
			{
				nums+=x16;
				lcd_draw_page(3);
				flag[11]++;
			}
        }
		if(r1==22)
		{
			if(pages%3==0)
			{
				nums+=x5;
				lcd_draw_page(1);
				flag[12]++;
			}
			if(pages%3==1)
			{
				nums+=x11;
				lcd_draw_page(2);
				flag[13]++;
			}
			if(pages%3==2)
			{
				nums+=x17;
				lcd_draw_page(3);
				flag[14]++;
			}
        }
		if(r1==23)
		{
			if(pages%3==0)
			{
				nums+=x6;
				lcd_draw_page(1);
				flag[15]++;
			}
			if(pages%3==1)
			{
				nums+=x11;
				lcd_draw_page(2);
				flag[16]++;
			}
			if(pages%3==2)
			{
				nums+=x18;
				lcd_draw_page(3);
				flag[17]++;
			}
        }
		//点菜减1
		if(r1==24)
		{
			if(pages%3==0)
			{
				flag[0]--;
				if(flag[0]>=0)
				{
				nums-=x1;
				}
				lcd_draw_page(1);
			}
			if(pages%3==1)
			{
				flag[1]--;
				if(flag[1]>0)
				{
				nums-=x7;
				}
				lcd_draw_page(2);
			}
			if(pages%3==2)
			{
				flag[2]--;
				if(flag[2]>0)
				{
				nums-=x13;
				}
				lcd_draw_page(3);
			}
        }
		if(r1==25)
		{
			if(pages%3==0)
			{
				flag[3]--;
				if(flag[3]>0)
				{
				nums-=x2;
				}
				lcd_draw_page(1);
			}
			if(pages%3==1)
			{
				flag[4]--;
				if(flag[4]>0)
				{
				nums-=x8;
				}
				lcd_draw_page(2);
			}
			if(pages%3==2)
			{
				flag[5]--;
				if(flag[5]>0)
				{
				nums-=x14;
				}
				lcd_draw_page(3);
			}
        }
		if(r1==26)
		{
            printf("3");
				if(pages%3==0)
			{
				flag[6]--;
				if(flag[6]>0)
				{
				nums-=x3;
				}
				lcd_draw_page(1);
			}
			if(pages%3==1)
			{
				flag[7]--;
				if(flag[7]>0)
				{
				nums-=x9;
				}
				lcd_draw_page(2);
			}
			if(pages%3==2)
			{
				flag[8]--;
				if(flag[8]>0)
				{
				nums-=x15;
				}
				lcd_draw_page(3);
			}
        }
		if(r1==27)
		{
			if(pages%3==0)
			{
				flag[9]--;
				if(flag[9]>0)
				{
				nums-=x4;
				}
				lcd_draw_page(1);
			}
			if(pages%3==1)
			{
				flag[10]--;
				if(flag[10]>0)
				{
				nums-=x10;
				}
				lcd_draw_page(2);
			}
			if(pages%3==2)
			{
				flag[11]--;
				if(flag[11]>0)
				{
				nums-=x16;
				}
				lcd_draw_page(3);
			}
        }
		if(r1==28)
		{
			if(pages%3==0)
			{
				flag[12]--;
				if(flag[12]>0)
				{
				nums-=x5;
				}
				lcd_draw_page(1);
			}
			if(pages%3==1)
			{
				flag[13]--;
				if(flag[13]>0)
				{
				nums-=x11;
				}
				lcd_draw_page(2);
			}
			if(pages%3==2)
			{
				flag[14]--;
				if(flag[14]>0)
				{
				nums-=x17;
				}
				lcd_draw_page(3);
			}
        }
		if(r1==29)
		{
			if(pages%3==0)
			{
				flag[15]--;
				if(flag[15]>0)
				{
				nums-=x6;
				}
				lcd_draw_page(1);
			}
			if(pages%3==1)
			{
				flag[16]--;
				if(flag[16]>0)
				{
				nums-=x11;
				}
				lcd_draw_page(2);
			}
			if(pages%3==2)
			{
				flag[17]--;
				if(flag[17]>0)
				{
					nums-=x18;
				}
				lcd_draw_page(3);
			}
        }
}

//打印第n页
void lcd_draw_page(int p)
{
	int a=p;
	if(a==1)
	{
		lcd_clear(0x00FFFFFF);
		lcd_draw_button();
		lcd_draw_num2(nums,72,3,0x00000000);
		lcd_draw_bmp(pics1[0],220+180*0,95);
		lcd_draw_bmp(pics1[1],220+180*1,95);
		lcd_draw_bmp(pics1[2],220+180*2,95);
		lcd_draw_bmp(pics1[3],220+180*0,265);
		lcd_draw_bmp(pics1[4],220+180*1,265);
		lcd_draw_bmp(pics1[5],220+180*2,265);
		lcd_draw_num(1,428,416,0x0000FF00);
		//画价格菜名
		for (int no0 = 0; no0 <= 6; no0++)
		{
			lcd_draw_word(z0[no0],220+24*no0,235,24,21,0x00000000);
		}
		for (int no1 = 0; no1 <= 6; no1++)
		{
			lcd_draw_word(z1[no1],400+24*no1,235,24,21,0x00000000);
		}
		for (int no2 = 0; no2 <= 6; no2++)
		{
			lcd_draw_word(z2[no2],580+24*no2,235,24,21,0x00000000);
		}
		for (int no3 = 0; no3 <= 7; no3++)
		{
			lcd_draw_word(z3[no3],200+24*no3,405,24,21,0x00000000);
		}
		for (int no4 = 0; no4 <= 5; no4++)
		{
			lcd_draw_word(z4[no4],420+24*no4,405,24,21,0x00000000);
		}
		for (int no5 = 0; no5 <= 6; no5++)
		{
			lcd_draw_word(z5[no5],580+24*no5,405,24,21,0x00000000);
		}
	}
	if(a==2)
	{
		lcd_clear(0x00FFFFFF);
		lcd_draw_button();
		lcd_draw_num2(nums,72,3,0x00000000);
		lcd_draw_bmp2(pics1[6],220+180*0,95);
		lcd_draw_bmp2(pics1[7],220+180*1,95);
		lcd_draw_bmp(pics1[8],220+180*2,95);
		lcd_draw_bmp(pics1[9],220+180*0,265);
		lcd_draw_bmp2(pics1[10],220+180*1,265);
		lcd_draw_bmp2(pics1[11],220+180*2,265);
		lcd_draw_num(2,428,416,0x0000FF00);
		//画价格菜名
		for (int no6 = 0; no6 <= 6; no6++)
		{
			lcd_draw_word(z6[no6],220+24*no6,235,24,21,0x00000000);
		}
		for (int no7 = 0; no7 <= 6; no7++)
		{
			lcd_draw_word(z7[no7],400+24*no7,235,24,21,0x00000000);
		}
		for (int no8 = 0; no8 <= 6; no8++)
		{
			lcd_draw_word(z8[no8],580+24*no8,235,24,21,0x00000000);
		}
		for (int no9 = 0; no9 <= 5; no9++)
		{
			lcd_draw_word(z9[no9],230+24*no9,405,24,21,0x00000000);
		}
		for (int no10 = 0; no10 <= 6; no10++)
		{
			lcd_draw_word(z10[no10],400+24*no10,405,24,21,0x00000000);
		}
		for (int no11 = 0; no11 <= 6; no11++)
		{
			lcd_draw_word(z11[no11],580+24*no11,405,24,21,0x00000000);
		}
	}
	if(a==3)
	{
		lcd_clear(0x00FFFFFF);
		lcd_draw_button();
		lcd_draw_num2(nums,72,3,0x00000000);
		lcd_draw_bmp2(pics1[12],220+180*0,95);
		lcd_draw_bmp(pics1[13],220+180*1,95);
		lcd_draw_bmp(pics1[14],220+180*2,95);
		lcd_draw_bmp(pics1[15],220+180*0,265);
		lcd_draw_bmp(pics1[16],220+180*1,265);
		lcd_draw_bmp2(pics0[2],220+180*2,265);
		lcd_draw_num(3,428,416,0x0000FF00);
		//画价格菜名
		for (int no12 = 0; no12 <= 6; no12++)
		{
			lcd_draw_word(z12[no12],220+24*no12,235,24,21,0x00000000);
		}
		for (int no13 = 0; no13 <= 5; no13++)
		{
			lcd_draw_word(z13[no13],420+24*no13,235,24,21,0x00000000);
		}
		for (int no14 = 0; no14 <= 6; no14++)
		{
			lcd_draw_word(z14[no14],580+24*no14,235,24,21,0x00000000);
		}
		for (int no15 = 0; no15 <= 6; no15++)
		{
			lcd_draw_word(z15[no15],220+24*no15,405,24,21,0x00000000);
		}
		for (int no16 = 0; no16 <= 7; no16++)
		{
			lcd_draw_word(z16[no16],390+24*no16,405,24,21,0x00000000);
		}
		for (int no17 = 0; no17 <= 6; no17++)
		{
			lcd_draw_word(zrn[no17],580+24*no17,405,24,21,0x00000000);
		}
	}
		
}

//主函数

int main()
{
	//初始化屏幕
	lcd_init();
	//先把屏幕刷成白色
	lcd_clear(0x00FFFFFF);
	lcd_draw_button();
	//输出点单价格
	lcd_draw_num2(nums,72,3,0x00000000);


		// 第一页菜单
	for (int i = 0; i <= 2; i++) 
		{
			lcd_draw_bmp(pics1[i],220+180*i,95);
		}
    lcd_draw_bmp(pics1[3],220+180*0,265);
	lcd_draw_bmp(pics1[4],220+180*1,265);
	lcd_draw_bmp(pics1[5],220+180*2,265);
				
	//画页数
	lcd_draw_num(1,428,416,0x0000FF00);
	//画价格菜名
	for (int no0 = 0; no0 <= 6; no0++)
	{
		lcd_draw_word(z0[no0],220+24*no0,235,24,21,0x00000000);
	}
	for (int no1 = 0; no1 <= 6; no1++)
	{
		lcd_draw_word(z1[no1],400+24*no1,235,24,21,0x00000000);
	}
	for (int no2 = 0; no2 <= 6; no2++)
	{
		lcd_draw_word(z2[no2],580+24*no2,235,24,21,0x00000000);
	}
	for (int no3 = 0; no3 <= 7; no3++)
	{
		lcd_draw_word(z3[no3],200+24*no3,405,24,21,0x00000000);
	}
	for (int no4 = 0; no4 <= 5; no4++)
	{
		lcd_draw_word(z4[no4],400+24*no4,405,24,21,0x00000000);
	}
	for (int no5 = 0; no5 <= 6; no5++)
	{
		lcd_draw_word(z5[no5],580+24*no5,405,24,21,0x00000000);
	}

	while(1)   
	{
		button();
		int sockfd = Init_TcpSocket();


    while(1)
    {

        write(sockfd,flag,strlen(flag));//给服务器发送数据

        sleep(1);

    }
		
		
    }

	//关闭屏幕
	lcd_close();
	return 0;
}


